package com.example.demo.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.entity.AccessoriesChannels;
import com.example.demo.entity.AccessoriesChannels2;
import com.example.demo.entity.AfterSalefront;
import com.example.demo.entity.AfterSalefront2;
import com.example.demo.entity.SysUser;

/**
 * 售后前台维修信息计算
 * 
 * @author DELL
 *
 */
@Service
public class AfterSaleFrontWeiXiuComputerService {
	@Autowired
	private AfterSalefrontService afterService1;
	@Autowired
	private AfterSalefront2Service after2Service2;
	/**
	 * 配件加价率
	 */
	@Autowired
	private AccessoriesChannels2Service acc2Service;
	@Autowired
	private AccessoriesChannelsService accService;

	/**
	 * @param user
	 * @return 保养进厂台次
	 */
	public Integer baoYangJinChangNums(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Integer num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {
				if (AfterSalefront2.getBaoyangClass() != null) {
					if (AfterSalefront2.getBaoyangClass().length() > 1) {
						num++;
					}

				}
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 机电进厂台次
	 */
	public Integer jiDianJinChangNums(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Integer num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {
				if (AfterSalefront2.getJidianClass() != null) {
					if (AfterSalefront2.getJidianClass().length() > 1) {
						num++;
					}
				}
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 钣喷进厂台次
	 */
	public Integer banPenJinChangNums(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Integer num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {
				if (AfterSalefront2.getBanpenClass() != null) {
					if (AfterSalefront2.getBanpenClass().length() > 1) {
						num++;
					}
				}
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 洗美进厂台次
	 */
	public Integer xiMeiJinChangNums(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Integer num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {
				if (AfterSalefront2.getXimeiClass() != null) {

					if (AfterSalefront2.getBanpenClass().length() > 1) {
						num++;
					}
				}
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 免费检查台次
	 */
	public Integer mianJianJinChangNums(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Integer num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {
				if (AfterSalefront2.getJidianClass() != null) {

					String[] split = AfterSalefront2.getJidianClass().split(",");
					for (String string : split) {
						if ("免检".equals(string)) {
							num++;
						}
					}
				}
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 事故车台次
	 */
	public Integer ShiguCheJinChangNums(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Integer num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {
				if (AfterSalefront2.getJidianClass() != null) {

					String[] split = AfterSalefront2.getJidianClass().split(",");
					for (String string : split) {
						if ("事故".equals(string)) {
							num++;
						}
					}
				}
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 保险车台次
	 */
	public Integer baoXianCheJinChangNums(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Integer bjyq = 0;
		Integer bj = 0;
		Integer yq = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {
				if (AfterSalefront2.getJidianClass() != null) {

					String[] split = AfterSalefront2.getJidianClass().split(",");
					for (String string : split) {
						if ("保险钣金&油漆".equals(string)) {
							bjyq++;
						}
						if ("保险钣金".equals(string)) {
							bj++;
						}
						if ("保险油漆".equals(string)) {
							yq++;
						}
					}
				}
			}
		}
		Integer num = bjyq + bj + yq;
		return num;
	}

	/**
	 * @param user
	 * @return 保险事故车台次
	 */
	public Integer baoxianShiGuCheNums(SysUser user, Date beginDate, Date endDate) {
		return ShiguCheJinChangNums(user, beginDate, endDate) + baoXianCheJinChangNums(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @return 索赔&召回台次
	 */
	public Integer suoPeiAndZhaoHuiNums(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Integer num1 = 0;
		Integer num2 = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {
				if (AfterSalefront2.getJidianClass() != null) {

					String[] split = AfterSalefront2.getJidianClass().split(",");
					for (String string : split) {
						if ("索赔&召回".equals(string)) {
							num1++;
						}
					}
				}
				if (AfterSalefront2.getBanpenClass() != null) {

					String[] split2 = AfterSalefront2.getBanpenClass().split(",");
					for (String string2 : split2) {
						if ("索赔&召回".equals(string2)) {
							num2++;
						}
					}
				}
			}
		}
		Integer num = num1 + num2;
		return num;
	}

	/**
	 * @param user
	 * @return 新车准备台次
	 */
	public Integer xinCheZhunBeiNums(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Integer num = 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {
				if (AfterSalefront2.getJidianClass() != null) {

					String[] split = AfterSalefront2.getJidianClass().split(",");
					for (String string : split) {
						if ("PDI".equals(string)) {
							num++;
						}
					}
				}
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 保养进厂占比
	 */
	public Float baoYangJinChangZhanBi(SysUser user, Date beginDate, Date endDate) {
		return (float) (baoYangJinChangNums(user, beginDate, endDate) / jinChangHejiNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @return 机电进厂占比
	 */
	public Float jiDianJinChangZhanBi(SysUser user, Date beginDate, Date endDate) {
		return (float) (jiDianJinChangNums(user, beginDate, endDate) / jinChangHejiNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @return 钣喷进厂占比
	 */
	public Float banPenJinChangZhanBi(SysUser user, Date beginDate, Date endDate) {
		return (float) (banPenJinChangNums(user, beginDate, endDate) / jinChangHejiNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @return 洗美进厂占比
	 */
	public Float xiMeiJinChangZhanBi(SysUser user, Date beginDate, Date endDate) {
		return (float) (xiMeiJinChangNums(user, beginDate, endDate) / jinChangHejiNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @return 保险事故进厂占比
	 */
	public Float baoXianShiGuJinChangZhanBi(SysUser user, Date beginDate, Date endDate) {
		return (float) (baoxianShiGuCheNums(user, beginDate, endDate) / jinChangHejiNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @return 索赔&召回进厂占比
	 */
	public Float suoPeiAndZhaoHuiJinChangZhanBi(SysUser user, Date beginDate, Date endDate) {
		return (float) (suoPeiAndZhaoHuiNums(user, beginDate, endDate) / jinChangHejiNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @return 免费检查进厂占比
	 */
	public Float mianJianZhanBi(SysUser user, Date beginDate, Date endDate) {
		return (float) (mianJianJinChangNums(user, beginDate, endDate) / jinChangHejiNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @return 新车准备台次
	 */
	public Float xinCheZhunBeiZhanBi(SysUser user, Date beginDate, Date endDate) {
		return (float) (xinCheZhunBeiNums(user, beginDate, endDate) / jinChangHejiNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @return 进厂台次合计
	 */
	public Integer jinChangHejiNums(SysUser user, Date beginDate, Date endDate) {
		return baoYangJinChangNums(user, beginDate, endDate) + jiDianJinChangNums(user, beginDate, endDate)
				+ banPenJinChangNums(user, beginDate, endDate) + xiMeiJinChangNums(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @return 保养配件收入
	 */
	public Float baoYangPeiJianPrice(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null && AfterSalefront2.getBaoYangpeijianPrice() != null) {

				Float aa = AfterSalefront2.getBaoYangpeijianPrice();
				num += aa;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 机电配件收入
	 */
	public Float jiDianPeiJianPrice(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null && AfterSalefront2.getJidianPeijianPrice() != null) {

				Float aa = AfterSalefront2.getJidianPeijianPrice();
				num += aa;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 钣喷配件收入
	 */
	public Float banPenPeiJianPrice(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null && AfterSalefront2.getBanpenPeijianPrice() != null) {

				Float aa = AfterSalefront2.getBanpenPeijianPrice();
				num += aa;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 洗美配件收入
	 */
	public Float xiMeiPeiJianPrice(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null && AfterSalefront2.getXimeiPeijianPrice() != null) {

				Float aa = AfterSalefront2.getXimeiPeijianPrice();
				num += aa;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @return 配件收入总计
	 */
	public Integer peiJianShouRuZongJi(SysUser user, Date beginDate, Date endDate) {
		return (int) (baoYangPeiJianPrice(user, beginDate, endDate) + jiDianPeiJianPrice(user, beginDate, endDate)
				+ xiMeiPeiJianPrice(user, beginDate, endDate) + banPenPeiJianPrice(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @return 保险事故车配件收入
	 */
	public Integer baoXianShiGuChePeiJian(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		Float num2 = (float) 0;
		Float num3 = (float) 0;
		Float num4 = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {

				num += AfterSalefront2.getShiguPeijian();
				num2 += AfterSalefront2.getBxbjPeijian();
				num3 += AfterSalefront2.getBxyqPeijian();
				num4 += AfterSalefront2.getBxbjyqPeijian();
			}
		}
		Integer nums = (int) (num + num2 + num3 + num4);
		return nums;
	}

	/**
	 * @param user
	 * @return 索赔&召回配件收入
	 */
	public Integer suoPeiPeiJian(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		Float num2 = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);

			num += AfterSalefront2.getSpPeijian();

			num2 += AfterSalefront2.getSuopeiPeijian();
		}
		Integer nums = (int) (num + num2);
		return nums;
	}

	/**
	 * @param user
	 * @return 新车准备配件收入
	 */
	public Float xinCheZhunBeiPeiJian(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getPdiPeijian() != null) {
				num += AfterSalefront2.getPdiPeijian();
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 保养工时收入
	 */
	public Float baoyangGongshiShouRu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getBaoYangPrice() != null) {
				num += AfterSalefront2.getBaoYangPrice();
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 计算机电工时收入
	 */
	public Float jiDianGongshiShouRu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getJidianPrice() != null) {
				num += AfterSalefront2.getJidianPrice();
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 钣喷工时收入
	 */
	public Float banPenGongshiShouRu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getBanpenPrice() != null) {
				num += AfterSalefront2.getBanpenPrice();
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 洗美工时收入
	 */
	public Float xiMeiGongshiShouRu(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getXimeiPrice() != null) {
				num += AfterSalefront2.getXimeiPrice();
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 工时收入合计
	 */
	public Integer gongShiShouRuZongJi(SysUser user, Date beginDate, Date endDate) {
		return (int) (baoyangGongshiShouRu(user, beginDate, endDate) + jiDianGongshiShouRu(user, beginDate, endDate)
				+ banPenGongshiShouRu(user, beginDate, endDate) + xiMeiGongshiShouRu(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 保险事故车工时收入
	 */
	public Integer baoXianShiGuCheGongShi(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		Float num2 = (float) 0;
		Float num3 = (float) 0;
		Float num4 = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {

				num += AfterSalefront2.getShiguGongshi();
				num2 += AfterSalefront2.getBxbjGongshi();
				num3 += AfterSalefront2.getBxyqGongshi();
				num4 += AfterSalefront2.getBxbjyqGongshi();
			}
		}
		Integer nums = (int) (num + num2 + num3 + num4);
		return nums;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 索赔&召回工时收入
	 */
	public Integer suoPeiGongShi(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		Float num2 = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			num += AfterSalefront2.getSpGongshi();
			num2 += AfterSalefront2.getSuopeiGongshi();
		}
		Integer nums = (int) (num + num2);
		return nums;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 新车准备工时收入
	 */
	public Integer xinCheZhunBeiGongShi(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		Float num2 = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2.getPdiGongshi() != null) {

				num += AfterSalefront2.getPdiGongshi();
			}
		}
		Integer nums = (int) (num + num2);
		return nums;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 保养收入合计
	 */
	public Float baoYangHeJi(SysUser user, Date beginDate, Date endDate) {
		return (baoyangGongshiShouRu(user, beginDate, endDate) + baoYangPeiJianPrice(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 机电收入合计
	 */
	public Integer jiDianHeJi(SysUser user, Date beginDate, Date endDate) {
		return (int) (jiDianGongshiShouRu(user, beginDate, endDate) + jiDianPeiJianPrice(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 钣喷收入合计
	 */
	public Integer banPenHeJi(SysUser user, Date beginDate, Date endDate) {
		return (int) (banPenGongshiShouRu(user, beginDate, endDate) + banPenPeiJianPrice(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 洗美收入合计
	 */
	public Integer xiMeiHeJi(SysUser user, Date beginDate, Date endDate) {
		return (int) (xiMeiGongshiShouRu(user, beginDate, endDate) + xiMeiPeiJianPrice(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 保险事故车收入合计
	 */
	public Integer baoXianShiGuCheHeJi(SysUser user, Date beginDate, Date endDate) {
		return baoXianShiGuCheGongShi(user, beginDate, endDate) + baoXianShiGuChePeiJian(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 索赔&召回收入合计
	 */
	public Integer suoPeiZhaoHuiShouRuHeJi(SysUser user, Date beginDate, Date endDate) {
		return suoPeiGongShi(user, beginDate, endDate) + suoPeiPeiJian(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 新车准备收入合计
	 */
	public Integer xinCheZhunBeiShouRuHeji(SysUser user, Date beginDate, Date endDate) {
		return (int) (xinCheZhunBeiGongShi(user, beginDate, endDate) + xinCheZhunBeiPeiJian(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后维修收入合计（保养收入+机电收入+钣喷收入+洗美收入）
	 */
	public Float ShouHouWeiXiuShouRuHeJi(SysUser user, Date beginDate, Date endDate) {
		return baoYangHeJi(user, beginDate, endDate) + jiDianHeJi(user, beginDate, endDate)
				+ banPenHeJi(user, beginDate, endDate) + xiMeiHeJi(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 保养收入占比（保养收入合计/售后维修收入合计）
	 */
	public Float baoYangShouRuZhanBi(SysUser user, Date beginDate, Date endDate) {
		if (ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate) != 0 && baoYangHeJi(user, beginDate, endDate) != 0) {
			float a = ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate);
			float b = baoYangHeJi(user, beginDate, endDate);
			float c = b / a;
			return (float) c;
		} else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 机电收入占比（机电收入合计/售后维修收入合计）
	 */
	public Float jiDianShouRuZhanBi(SysUser user, Date beginDate, Date endDate) {
		if(ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate)!=0&&jiDianHeJi(user, beginDate, endDate)!=0) {
			float a=ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate);
			float b=jiDianHeJi(user, beginDate, endDate);
			float c=b/a;
			return  (float) c;
		}else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 钣喷收入占比（钣喷收入合计/售后维修收入合计）
	 */
	public Float banPenShouRuZhanBi(SysUser user, Date beginDate, Date endDate) {
		if(ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate)!=0&&banPenHeJi(user, beginDate, endDate)!=0) {
			float a=ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate);
			float b=banPenHeJi(user, beginDate, endDate);
			float c=b/a;
			return  (float) c;
		}else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 洗美收入占比（洗美收入合计/售后维修收入合计）
	 */
	public Float xiMeiShouRuZhanBi(SysUser user, Date beginDate, Date endDate) {
		if(ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate)!=0&&xiMeiHeJi(user, beginDate, endDate)!=0) {
			float a=ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate);
			float b=xiMeiHeJi(user, beginDate, endDate);
			float c=b/a;
			return  (float) c;
		}else {
			return (float) 0;
		}
	
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 保险事故车收入占比（保险事故车收入合计/售后维修收入合计）
	 */
	public Float baoXianShiGuCheShouRuZhanBi(SysUser user, Date beginDate, Date endDate) {
		if(ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate)!=0&&baoXianShiGuCheHeJi(user, beginDate, endDate)!=0) {
			float a=ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate);
			float b=baoXianShiGuCheHeJi(user, beginDate, endDate);
			float c=b/a;
			return  (float) c;
		}else {
			return (float) 0;
		}
	
	
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 索赔&召回收入占比（索赔&召回收入合计/售后维修收入合计）
	 */
	public Float suoPeiZhaoHuiShouRuZhanBi(SysUser user, Date beginDate, Date endDate) {
		if(ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate)!=0&&suoPeiZhaoHuiShouRuHeJi(user, beginDate, endDate)!=0) {
			float a=ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate);
			float b=suoPeiZhaoHuiShouRuHeJi(user, beginDate, endDate);
			float c=b/a;
			return  (float) c;
		}else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 新车准备收入占比（新车准备收入合计/售后维修收入合计）
	 */
	public Float xinCheZhunBeiShouRuZhanBi(SysUser user, Date beginDate, Date endDate) {
		if(ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate)!=0&&xinCheZhunBeiShouRuHeji(user, beginDate, endDate)!=0) {
			float a=ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate);
			float b=xinCheZhunBeiShouRuHeji(user, beginDate, endDate);
			float c=b/a;
			return  (float) c;
		}else {
			return (float) 0;
		}
	
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 配件收入占比（配件收入合计/售后维修收入合计）
	 */
	public Float peiJianShouRuZhanBi(SysUser user, Date beginDate, Date endDate) {
		if(ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate)!=0&&peiJianShouRuZongJi(user, beginDate, endDate)!=0) {
			float a=ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate);
			float b=peiJianShouRuZongJi(user, beginDate, endDate);
			float c=b/a;
			return  (float) c;
		}else {
			return (float) 0;
		}
	
	
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 工时收入占比（工时收入合计/售后维修收入合计）
	 */
	public Float gongShiShouRuZhanBi(SysUser user, Date beginDate, Date endDate) {
		if(ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate)!=0&&gongShiShouRuZongJi(user, beginDate, endDate)!=0) {
			float a=ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate);
			float b=gongShiShouRuZongJi(user, beginDate, endDate);
			float c=b/a;
			return  (float) c;
		}else {
			return (float) 0;
		}
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 配件成本合计(配件收入/(配件加价率+1))
	 */
	public Float peiJianChenBen(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num1 = (float) 0;
		Float num2 = (float) 0;
		Float youyeChengben = (float) 0;
		Float lingJianChengben = (float) 0;
		Float yangHuPinChengben = (float) 0;
		Float jingPinChengben = (float) 0;
		Float qiLeiChengben = (float) 0;
		Float lunTaiChengben = (float) 0;
		Float diZhiYiHaoChengben = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null) {
				if ("本品牌".equals(AfterSalefront2.getChePinPai())) {
					if (AfterSalefront2.getPeijianClass() != null) {
						String[] split = AfterSalefront2.getPeijianClass().split(",");
						for (String string : split) {
							if ("油液类".equals(string)) {
								num2 = AfterSalefront2.getYouLeiPrice();
								List<AccessoriesChannels> accs = accService.findByName("原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("油液类") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("油液类");
											num1 = acc2.getMarkup();
											youyeChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("零件".equals(string)) {
								num2 = AfterSalefront2.getLingJianPrice();
								List<AccessoriesChannels> accs = accService.findByName("原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("零件") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("零件");
											num1 = acc2.getMarkup();
											lingJianChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("养护品".equals(string)) {
								num2 = AfterSalefront2.getYanghuPrice();
								List<AccessoriesChannels> accs = accService.findByName("原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("养护品") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("养护品");
											num1 = acc2.getMarkup();
											yangHuPinChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("精品".equals(string)) {
								num2 = AfterSalefront2.getJingpinPrice();
								List<AccessoriesChannels> accs = accService.findByName("原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("精品") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("精品");
											num1 = acc2.getMarkup();
											jingPinChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("轮胎".equals(string)) {
								num2 = AfterSalefront2.getLunTaiPrice();
								List<AccessoriesChannels> accs = accService.findByName("原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("轮胎") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("轮胎");
											num1 = acc2.getMarkup();
											lunTaiChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("漆类".equals(string)) {
								num2 = AfterSalefront2.getQiPrice();
								List<AccessoriesChannels> accs = accService.findByName("原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("漆类") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("漆类");
											num1 = acc2.getMarkup();
											qiLeiChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("低值易耗品".equals(string)) {
								num2 = AfterSalefront2.getDizhiyihaoPrice();
								List<AccessoriesChannels> accs = accService.findByName("原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("低值易耗品") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("低值易耗品");
											num1 = acc2.getMarkup();
											diZhiYiHaoChengben = num2 / (num1 + 1);
										}
									}
								}
							}
						}
					}

				}
				if ("外品牌".equals(AfterSalefront2.getChePinPai())) {
					if (AfterSalefront2.getPeijianClass() != null) {
						String[] split = AfterSalefront2.getPeijianClass().split(",");
						for (String string : split) {
							if ("油液类".equals(string)) {
								num2 = AfterSalefront2.getYouLeiPrice();
								List<AccessoriesChannels> accs = accService.findByName("非原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("油液类") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("油液类");
											num1 = acc2.getMarkup();
											youyeChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("零件".equals(string)) {
								num2 = AfterSalefront2.getLingJianPrice();
								List<AccessoriesChannels> accs = accService.findByName("非原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("零件") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("零件");
											num1 = acc2.getMarkup();
											lingJianChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("养护品".equals(string)) {
								num2 = AfterSalefront2.getYanghuPrice();
								List<AccessoriesChannels> accs = accService.findByName("非原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("养护品") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("养护品");
											num1 = acc2.getMarkup();
											yangHuPinChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("精品".equals(string)) {
								num2 = AfterSalefront2.getJingpinPrice();
								List<AccessoriesChannels> accs = accService.findByName("非原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("精品") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("精品");
											num1 = acc2.getMarkup();
											jingPinChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("轮胎".equals(string)) {
								num2 = AfterSalefront2.getLunTaiPrice();
								List<AccessoriesChannels> accs = accService.findByName("非原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("轮胎") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("轮胎");
											num1 = acc2.getMarkup();
											lunTaiChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("漆类".equals(string)) {
								num2 = AfterSalefront2.getQiPrice();
								List<AccessoriesChannels> accs = accService.findByName("非原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("漆类") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("漆类");
											num1 = acc2.getMarkup();
											qiLeiChengben = num2 / (num1 + 1);
										}
									}
								}
							}
							if ("低值易耗品".equals(string)) {
								num2 = AfterSalefront2.getDizhiyihaoPrice();
								List<AccessoriesChannels> accs = accService.findByName("非原厂");
								for (AccessoriesChannels accessoriesChannels : accs) {
									List<AccessoriesChannels2> list2 = acc2Service
											.findByAccessoriesChannels(accessoriesChannels);
									for (AccessoriesChannels2 accessoriesChannels2 : list2) {
										if (acc2Service.findByCategory("低值易耗品") != null) {
											AccessoriesChannels2 acc2 = acc2Service.findByCategory("低值易耗品");
											num1 = acc2.getMarkup();
											diZhiYiHaoChengben = num2 / (num1 + 1);
										}
									}
								}
							}
						}
					}

				}
			}
		}
		return youyeChengben + lingJianChengben + yangHuPinChengben + jingPinChengben + lunTaiChengben + qiLeiChengben
				+ diZhiYiHaoChengben;

	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 工时成本(外协成本)
	 */
	public Float gongShiChengBen(SysUser user, Date beginDate, Date endDate) {
		List<AfterSalefront> list = afterService1.findBySysuser(user);
		Float num = (float) 0;
		for (AfterSalefront AfterSalefront : list) {
			AfterSalefront2 AfterSalefront2 = findByAfterSale1(AfterSalefront, beginDate, endDate);
			if (AfterSalefront2 != null && AfterSalefront2.getWaixieCost() != null) {

				Integer aa = AfterSalefront2.getWaixieCost();
				num += aa;
			}
		}
		return num;
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 工时毛利合计(工时收入总计-工时成本总计)
	 */
	public Float gongShiMaoLi(SysUser user, Date beginDate, Date endDate) {
		return gongShiShouRuZongJi(user, beginDate, endDate) - gongShiChengBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后维修成本合计(配件成本合计+工时成本合计)
	 */
	public Float weiXiuChengBen(SysUser user, Date beginDate, Date endDate) {
		return peiJianChenBen(user, beginDate, endDate) + gongShiChengBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 售后维修毛利合计
	 */
	public Float weiXiuMaoLi(SysUser user, Date beginDate, Date endDate) {
		return ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate) - weiXiuChengBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 配件毛利合计(配件收入合计-配件成本合计)
	 */
	public Float peiJianMaoLi(SysUser user, Date beginDate, Date endDate) {
		return peiJianShouRuZongJi(user, beginDate, endDate) - peiJianChenBen(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 配件毛利率(配件毛利合计/配件收入合计)
	 */
	public Float peiJianMaoLiLv(SysUser user, Date beginDate, Date endDate) {
		return peiJianMaoLi(user, beginDate, endDate) / peiJianShouRuZongJi(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 工时毛利率(工时毛利合计/工时收入合计)
	 */
	public Float gongShiMaoLiLv(SysUser user, Date beginDate, Date endDate) {
		return gongShiMaoLi(user, beginDate, endDate) / gongShiShouRuZongJi(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 维修毛利率(维修毛利合计/售后维修收入合计)
	 */
	public Float weiXiuMaoLiLv(SysUser user, Date beginDate, Date endDate) {
		return weiXiuMaoLi(user, beginDate, endDate) / ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate);
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 保养单车产值(保养收入合计/保养进厂台次)
	 */
	public Float baoYangDanCheChanZhi(SysUser user, Date beginDate, Date endDate) {
		return (float) (baoYangHeJi(user, beginDate, endDate) / baoYangJinChangNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 机修单车产值(机电收入合计/机电进厂台次)
	 */
	public Float jiXiuDanCheChanZhi(SysUser user, Date beginDate, Date endDate) {
		return (float) (jiDianHeJi(user, beginDate, endDate) / jiDianJinChangNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 钣喷单车产值(钣喷收入合计/钣喷进厂台次)
	 */
	public Float banPenDanCheChanZhi(SysUser user, Date beginDate, Date endDate) {
		return (float) (banPenHeJi(user, beginDate, endDate) / banPenJinChangNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 洗美单车产值(洗美收入合计/洗美进厂台次)
	 */
	public Float xiMeiDanCheChanZhi(SysUser user, Date beginDate, Date endDate) {
		return (float) (xiMeiHeJi(user, beginDate, endDate) / xiMeiJinChangNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 保险事故车单车产值(保险事故车收入合计/保险事故车进厂台次)
	 */
	public Float baoXianShiGuCheDanCheChanZhi(SysUser user, Date beginDate, Date endDate) {
		return (float) (baoXianShiGuCheHeJi(user, beginDate, endDate) / baoxianShiGuCheNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 索赔&召回单车产值(索赔&召回收入合计/索赔&召回进厂台次)
	 */
	public Float suoPeiZhaoHuiDanCheChanZhi(SysUser user, Date beginDate, Date endDate) {
		return (float) (suoPeiZhaoHuiShouRuHeJi(user, beginDate, endDate)
				/ suoPeiAndZhaoHuiNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return 新车准备单车产值(新车准备收入合计/新车准备进厂台次)
	 */
	public Float xinCheZhunBeiDanCheChanZhi(SysUser user, Date beginDate, Date endDate) {
		return (float) (xinCheZhunBeiShouRuHeji(user, beginDate, endDate)
				/ xinCheZhunBeiNums(user, beginDate, endDate));
	}

	/**
	 * @param user
	 * @param beginDate
	 * @param endDate
	 * @return `售后维修单车产值(售后维修收入合计/售后维修进厂台次)
	 */
	public Float shouHouWeiXiuDanCheChanZhi(SysUser user, Date beginDate, Date endDate) {
		return (float) (ShouHouWeiXiuShouRuHeJi(user, beginDate, endDate) / jinChangHejiNums(user, beginDate, endDate));
	}

	/**
	 * @param sale1
	 * @return 通过售后前台1表找2表
	 */
	public AfterSalefront2 findByAfterSale1(AfterSalefront sale1, Date beginDate, Date endDate) {
		List<AfterSalefront2> sales2 = after2Service2.findByAfterSalefront(sale1);
		if (sales2 != null) {
			AfterSalefront2 sales = new AfterSalefront2();
			Integer id = 0;
			for (AfterSalefront2 y : sales2) {
				if (id < y.getId()) {
					id = y.getId();
				}
			}
			sales = after2Service2.findById(id);
			boolean b = isEffectiveDate(sales.getBeginDate(), beginDate, endDate);
			if (b) {
				return sales;
			} else {
				return null;
			}

		} else {
			return null;
		}
	}

	/**
	 * 
	 * @param nowTime   当前时间
	 * @param startTime 开始时间
	 * @param endTime   结束时间
	 * @return
	 * @author 判断当前时间在时间区间内
	 */
	public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
		if (nowTime.getTime() == startTime.getTime() || nowTime.getTime() == endTime.getTime()) {
			return true;
		}

		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);

		Calendar begin = Calendar.getInstance();
		begin.setTime(startTime);

		Calendar end = Calendar.getInstance();
		end.setTime(endTime);

		if (date.after(begin) && date.before(end)) {
			return true;
		} else {
			return false;
		}
	}
}
